﻿using System;

// XNA lib
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

//Oak Engine Lib
using OakEngine.GameState;
using OakEngine.Input;
using Microsoft.Xna.Framework.Input;
using System.IO;

namespace MainJumpFleet.GameStates
{


    // Look into threading http://msdn.microsoft.com/en-us/library/bb203914.aspx

    public class MainMenuState : OAK_IGameState
    {
        // have a texture manager call here just to call the texture and thats it
        Texture2D m_MainMenuTexture;
        Texture2D dotTexture;
        SpriteBatch batch;
        SpriteFont mainMenuFont;


        // Main Playground
        string szMainPlayground = "Main Playground";
        Vector2 mainPlaygroundPosition;

        // Camera Playground
        string szCameraPlayground = "Camera Playground";
        Vector2 cameraPlaygroundPosition;

        // Input & Debug Playground
        string szInputDebugPlayground = "Input & Debug Playground";
        Vector2 inputDebugPlaygroundPosition;

        // Movement & AI Playground
        string szMovementAIPlayground = "Movement AI Playground";
        Vector2 movementAIPlaygroundPosition;

        // Shooting Playground
        string szShootingPlayground = "Shooting Playground";
        Vector2 shootingPlaygroundPosition;

        // Large Sector Map Playground
        string szLargeSectorMapPlayground = "Large Sector Map Playground";
        Vector2 largeSectorMapPlaygroundPosition;

        // Level Design Playground
        string szLevelDesignPlayground = "Level Design Playground";
        Vector2 levelDesignPlaygroundPosition;

        // Level Design Playground
        string szNewCameraPlayground = "New Camera Playground";
        Vector2 newCameraPlaygroundPosition;

        // Prototype Design Playground
        string szPrototypePlayground = "New Prototype Playground";
        Vector2 PrototypePlaygroundPosition;

        // Fleet Rotation Playground
        string szFleetRotationPlayground = "Fleet Rotation Playground";
        Vector2 FleetRotationPlaygroundPosition;

        // New Ship Movement Playground
        string szNewShipMovementPlayground = "New Ship Movement Playground";
        Vector2 NewShipMovementPlaygroundPosition;


        int m_fSelection;

        public MainMenuState()
        {
            m_MainMenuTexture = CGame.CGame.GetInstance().Content.Load<Texture2D>("Screens\\MainMenuScreen");
            dotTexture = CGame.CGame.GetInstance().Content.Load<Texture2D>("Debug\\dot");
            mainMenuFont = CGame.CGame.GetInstance().Content.Load<SpriteFont>("Fonts\\mainMenuFont");
            batch = CGame.CGame.GetInstance().SpriteBatch;

            m_fSelection = 100;
            Enter();

        }



        #region OAK_IGameState Members

        public void Enter()
        {
            mainPlaygroundPosition = new Vector2(150, 100);
            cameraPlaygroundPosition = new Vector2(150, 150);
            inputDebugPlaygroundPosition = new Vector2(150, 200);
            movementAIPlaygroundPosition = new Vector2(150, 250);
            shootingPlaygroundPosition = new Vector2(150, 300);
            largeSectorMapPlaygroundPosition = new Vector2(150, 350);
            levelDesignPlaygroundPosition = new Vector2(150, 400);
            newCameraPlaygroundPosition = new Vector2(150, 450);
            PrototypePlaygroundPosition = new Vector2(150, 500);
            FleetRotationPlaygroundPosition = new Vector2(150, 550);
            NewShipMovementPlaygroundPosition = new Vector2(150, 600);
        }

        public bool Input(OAK_InputManager input)
        {
            if ((input.Pressed(Keys.S) || input.Pressed(Buttons.DPadDown, 0) || input.Pressed(Keys.Down)))
                m_fSelection += 50;
            if ((input.Pressed(Keys.W) || input.Pressed(Buttons.DPadUp, 0) || input.Pressed(Keys.Up)))
                m_fSelection -= 50;

            if (input.Pressed(Keys.P))
                SaveTexture2Target();


            if (input.Pressed(Keys.Enter) || input.Pressed(Buttons.Start, 0) || input.Pressed(Buttons.A, 0))
                switch (m_fSelection)
                {
                    case 100:
                        CGame.CGame.GetInstance().ChangeState(new MainGameScreenState());
                        return true;

                    case 150:
                        CGame.CGame.GetInstance().ChangeState(new CameraMenuScreenState());
                        return true;

                    case 200:
                        CGame.CGame.GetInstance().ChangeState(new InputMenuScreenState());
                        return true;

                    case 250:
                        CGame.CGame.GetInstance().ChangeState(new MovementAIScreenState());
                        return true;

                    case 300:
                       // CGame.CGame.GetInstance().ChangeState(new ShootingGameScreenState());
                        return true;

                    case 350:
                        CGame.CGame.GetInstance().ChangeState(new LargeSectorMapGameScreenState());
                        return true;

                    case 400:
                        CGame.CGame.GetInstance().ChangeState(new LevelDesignGameScreenState());
                        return true;

                    case 450:
                       // CGame.CGame.GetInstance().ChangeState(new NewCameraTestScreenState());
                        return true;

                    case 500:
                        CGame.CGame.GetInstance().ChangeState(new PrototypePlaygroundScreenState());
                        return true;

                    case 550:
                        CGame.CGame.GetInstance().ChangeState(new FleetRotationGameScreenState());
                        return true;

                    case 600:
                        CGame.CGame.GetInstance().ChangeState(new NewShipMovementGameScreenState());
                        return true;

                    default:
                        return true;

                }

            return false;
        }

        public void Update(GameTime dt)
        {
            if (m_fSelection > 600)
                m_fSelection = 100;
            else if (m_fSelection < 100)
                m_fSelection = 600;

        }

        public void Render()
        {
            CGame.CGame.GetInstance().Graphics.GraphicsDevice.Clear(Color.Black);

            batch.Draw(m_MainMenuTexture, Vector2.Zero, Color.White);
            batch.Draw(dotTexture, new Vector2(100, m_fSelection), Color.White);

            batch.DrawString(mainMenuFont, szMainPlayground, mainPlaygroundPosition, Color.White);
            batch.DrawString(mainMenuFont, szCameraPlayground, cameraPlaygroundPosition, Color.White);
            batch.DrawString(mainMenuFont, szInputDebugPlayground, inputDebugPlaygroundPosition, Color.White);
            batch.DrawString(mainMenuFont, szMovementAIPlayground, movementAIPlaygroundPosition, Color.White);
            // batch.DrawString(mainMenuFont, szShootingPlayground, shootingPlaygroundPosition, Color.White);
            batch.DrawString(mainMenuFont, szLargeSectorMapPlayground, largeSectorMapPlaygroundPosition, Color.White);
            batch.DrawString(mainMenuFont, szLevelDesignPlayground, levelDesignPlaygroundPosition, Color.White);
            // batch.DrawString(mainMenuFont, szNewCameraPlayground, newCameraPlaygroundPosition, Color.White);
            batch.DrawString(mainMenuFont, szPrototypePlayground, PrototypePlaygroundPosition, Color.White);
            batch.DrawString(mainMenuFont, szFleetRotationPlayground, FleetRotationPlaygroundPosition, Color.White);
            batch.DrawString(mainMenuFont, szNewShipMovementPlayground, NewShipMovementPlaygroundPosition, Color.White);
            batch.DrawString(mainMenuFont, "Press Enter, Start or A game pad button to select", Vector2.Zero, Color.White);
            
        }

        public void Exit()
        {

        }

        /// <summary>
        /// Look into this for making a texture and changing each pixel
        /// http://stackoverflow.com/questions/3255311/color-replacement-in-xna-c-sharp
        /// </summary>
        public void SaveTexture2Target()
        {
            RenderTarget2D sc = new RenderTarget2D(CGame.CGame.GetInstance().Device, 1024, 768);
            CGame.CGame.GetInstance().Device.SetRenderTarget(sc);

            //render
            CGame.CGame.GetInstance().Render();

            CGame.CGame.GetInstance().Device.SetRenderTarget(null);


            //save to disk
            Stream s = File.OpenWrite("testImage.jpg");
            sc.SaveAsJpeg(s, 1024, 768);
            s.Dispose();
            sc.Dispose();
        }

        public void SaveTexture()
        {
            int w = CGame.CGame.GetInstance().Device.PresentationParameters.BackBufferWidth;
            int h = CGame.CGame.GetInstance().Device.PresentationParameters.BackBufferHeight;

            // FOrce the frame to draw

            // Pull the picture from the buffer
            int[] backbuffer = new int[w * h];
            CGame.CGame.GetInstance().Device.GetBackBufferData(backbuffer);

            // Compy into a texture
            Texture2D texture = new Texture2D(CGame.CGame.GetInstance().Device,
                w, h, false, CGame.CGame.GetInstance().Device.PresentationParameters.BackBufferFormat);

            texture.SetData(backbuffer);

            //save to disk
            Stream s = File.OpenWrite("testImage.jpg");
            texture.SaveAsJpeg(s, w, h);
            s.Dispose();
            texture.Dispose();

        }

        #endregion
    }
}
